class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        TreeNode root = null;
        int size = inorder.length;
        for (int i = 0; i < size; i++) {
            if (inorder[i] == postorder[size - 1]) {
                root = new TreeNode(inorder[i]);
                root.left = buildTree(Arrays.copyOfRange(inorder, 0, i), Arrays.copyOfRange(postorder, 0, i));
                root.right = buildTree(Arrays.copyOfRange(inorder, i + 1, size), Arrays.copyOfRange(postorder, i, size - 1));
                break;
            }
        }
        return root;
    }
}


/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

    int post;


    public TreeNode buildTree(int[] inorder, int[] postorder) {
        post=postorder.length-1;

        TreeNode root=createTree(inorder,postorder,0,inorder.length-1);

        return root;


    }


    StringBuffer str=new StringBuffer();



    private int findIndex(int postVal,int[] inorder,int s,int e){
        for(int i=s;i<=e;i++){
            if(postVal==inorder[i]){
                return i;
            }
        }
        str.

        return -1;

    }





    private TreeNode createTree(int[] inorder, int[] postorder,int s,int e){
        if(s>e){
            return null;
        }

        int index=findIndex(postorder[post],inorder,s,e);
        TreeNode root=new TreeNode(postorder[post]);
        post--;

        root.right=createTree(inorder,postorder,index+1,e);
        root.left=createTree(inorder,postorder,s,index-1);
        return root;

    }


}